package com.example.diagnose.Config;

import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

@Configuration
@WebFilter(filterName = "CORSFilter")
public class CORDFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse)servletResponse;
        HttpServletRequest request  =(HttpServletRequest) servletRequest;


        //允许谁请求我（没限制就是*）
        response.setHeader("Access-Control-Allow-Origin",request.getHeader("Origin"));
        //允许传递cookies值
        response.setHeader("Access-Control-Allow-Credentials","true");
        //允许哪些方法跨域请求
        response.setHeader("Access-Control-Allow-Methods","POST,GET,PATCH,DELETE,PUT");
        //允许最大请求数量
        response.setHeader("Access-Control-Max-Age","3600");

        response.setHeader("Access-Control-Allow-Headers","Origin,X-Requested-with,Content-Type,Accept");

        filterChain.doFilter(request,response);

    }
}
